package gherkinedit.templatesystem;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import gherkinedit.utils.FilesystemUtils;
import com.thoughtworks.xstream.XStream;

public class XMLHandler {
	public static void saveTemplate(Template template) {
		XStream xstream = new XStream();
		xstream.alias("template", Template.class);
	    // xstream.addImplicitCollection(Template.class, "textList");
	    String xml = xstream.toXML(template);
	    String relativePath = "\\GherkinEditTemplates\\";
	    String filename = template.getName() + ".getemplate";
		FilesystemUtils.saveStringAsFileInProject(relativePath, filename, xml);   
	}
	
	public static List<Template> getTemplates() {
		List<File> templateFiles = FilesystemUtils.getFilesByExtension(FilesystemUtils.getCurrentProjectRootDirectory(), "getemplate");
		List<Template> templates = new ArrayList<Template>();
		XStream xstream = new XStream();
		xstream.setClassLoader(Template.getClassLoader());
		xstream.alias("template", Template.class);	
		for (File file : templateFiles) {
			templates.add((Template)xstream.fromXML(file));
		}
		
		return templates;
	}
}
